//
//  HomeDeliciousFoodController.m
//  OnionHome
//
//  Created by fengjie on 2019/9/19.
//  Copyright © 2019 FengJie. All rights reserved.
//

#import "HomeDeliciousFoodController.h"
#import "ShareViewController.h"
#import "HomeCommentsController.h"
#import "UserIconAndNameView.h"
#import "HomeDeliciousFoodDetailController.h"
#import "HomeDeliciousFoodModel.h"

@interface HomeDeliciousFoodController ()
/**listViewTool*/
@property (nonatomic,strong)UITableViewTool*listViewTool;
/**<#属性#>*/
@property (nonatomic,strong)NSMutableArray*dataSoure;

@end

@implementation HomeDeliciousFoodController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.listViewTool.listView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    [self setNotificationCenter];
}
-(void)setNotificationCenter{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadLocaData) name:K_ReleaseDeliciousFood object:nil];
}
-(void)reloadLocaData{
    self.dataSoure = nil;
    [self.listViewTool reloadData:self.dataSoure];
}
#pragma mark ---懒加载

-(UITableViewTool*)listViewTool{
    if(!_listViewTool){
        kWeakSelf(weakSelf);
        _listViewTool=[[UITableViewTool alloc] initWithFrame:CGRectZero style:UITableViewStylePlain registerCellClassName:NSStringFromClass([HomeDeliciousFoodCell class]) identifier:NSStringFromClass([HomeDeliciousFoodCell class]) dataArray:self.dataSoure delegate:self dataSourceBlock:^(id  _Nonnull TempCell, NSIndexPath * _Nonnull indexPath, id  _Nonnull TempData) {
            HomeDeliciousFoodCell * cell = (HomeDeliciousFoodCell*)TempCell;
            HomeDeliciousFoodModel * model = (HomeDeliciousFoodModel *)TempData;
            cell.model = model;
            
        } selectCell:^(id  _Nonnull TempCell, NSIndexPath * _Nonnull indexPath, id  _Nonnull TempData) {
            HomeDeliciousFoodDetailController * vc = [HomeDeliciousFoodDetailController new];
            vc.model = (HomeDeliciousFoodModel*)TempData;
            [weakSelf.navigationController pushViewController:vc animated:YES];
        }];
        _listViewTool.listView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _listViewTool;
}
-(NSMutableArray *)dataSoure{
    if (!_dataSoure) {
        _dataSoure =[[NSMutableArray alloc] init];
        
        NSMutableArray * oneArray = [PlistTool getPlistType:ReleseFood];
        
        [oneArray enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            HomeDeliciousFoodModel *model = [HomeDeliciousFoodModel new];
            [model setValuesForKeysWithDictionary:obj];
            [_dataSoure addObject:model];
        }];
        
        
        NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"HomeDeliciousFood" ofType:@"plist"];
               //当数据结构为数组时
            NSArray *array = [[NSArray alloc] initWithContentsOfFile:plistPath];
            
        [array enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {

            HomeDeliciousFoodModel *model = [HomeDeliciousFoodModel new];
            [model setValuesForKeysWithDictionary:obj];

            if (model.imageNumber.integerValue > 0) {
                NSMutableArray * imageArray = [NSMutableArray array];
                for (NSInteger i = 0 ; i < model.imageNumber.integerValue; i++) {
                    [imageArray addObject:[Utilis getRandomDynamicImage]];
                }

                model.contenImage = IMAGE_NAMED([Utilis getRandomDynamicImage]);
            }

            [_dataSoure addObject:model];
        }];
    }
    return _dataSoure;
}
@end

@interface HomeDeliciousFoodCell ()
/**<#属性#>*/
@property (nonatomic,strong)UIImageView*dynamicImage;
/**<#属性#>*/
@property (nonatomic,strong)UserIconAndNameView * userView;
/**<#属性#>*/
@property (nonatomic,strong)UIButton * measgBtn;
/**<#属性#>*/
@property (nonatomic,strong)UIButton * dianzanBtn;
/**菜系*/
@property (nonatomic,strong)UILabel * rightLabel;
@end

@implementation HomeDeliciousFoodCell


-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self createUI];
    }
    return self;
}
-(void)setModel:(HomeDeliciousFoodModel *)model{
    _model = model;
    self.dynamicImage.image = model.contenImage;
    self.userView.userName.text = model.name;
    [self.measgBtn setTitle:model.commentCount forState:UIControlStateNormal];
    [self.dianzanBtn setTitle:model.praiseCount forState:UIControlStateNormal];
    self.rightLabel.text = StrValid(model.styleOfCooking) ? model.styleOfCooking : @"川菜";
}
-(void)createUI{
    self.dynamicImage= UIImageView.new;
    self.dynamicImage.image = IMAGE_NAMED(@"dynamicDefut");
    
    [self addSubview:self.dynamicImage];
    
    self.rightLabel = UILabel.new;
    self.rightLabel.textColor = UIColorFromRGB(0xF5F7F5);
    self.rightLabel.font = PingFangSC_Medium(11);
    self.rightLabel.textAlignment = NSTextAlignmentCenter;
    self.rightLabel.text = @"川菜";
    
    
    [self.dynamicImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(16);
        make.height.mas_equalTo(190);
        make.right.mas_equalTo(-16);
    }];
    
    [self.dynamicImage addSubview:self.rightLabel];
    
    [self.rightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.bottom.mas_equalTo(-16);
    }];
    
    
    UIView * view = [self createBottomView];
    [self addSubview:view];
    
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.dynamicImage.mas_bottom);
        make.bottom.left.right.mas_equalTo(0);
    }];
    
    
}
-(UIView *)createBottomView{
    UIView * view = UIView.new;
    
    self.userView = [[UserIconAndNameView alloc] initWithFrame:CGRectZero];
    self.userView.deleButton.hidden = YES;
    self.userView.dynamicData.text = @"美食作家";
    [view addSubview:self.userView];
    
    
    self.measgBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.measgBtn setTitleColor:UIColorFromRGB(0x959E97) forState:UIControlStateNormal];
    [self.measgBtn setTitle:@"300" forState:UIControlStateNormal];
    self.measgBtn.titleLabel.font = SYSTEMFONT(12);
    [self.measgBtn setImage:IMAGE_NAMED(@"home_mesae") forState:UIControlStateNormal];
//    [self.measgBtn setImageEdgeInsets:UIEdgeInsetsMake(0, -5, 0, 10)];
    [self.measgBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 4, 0, -4)];
    
    self.dianzanBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.dianzanBtn setTitleColor:UIColorFromRGB(0x959E97) forState:UIControlStateNormal];
    [self.dianzanBtn setTitle:@"213" forState:UIControlStateNormal];
    self.dianzanBtn.titleLabel.font = SYSTEMFONT(12);
    [self.dianzanBtn setImage:IMAGE_NAMED(@"ic_like_selected1") forState:UIControlStateNormal];
//    [self.dianzanBtn setImageEdgeInsets:UIEdgeInsetsMake(0, -5, 0, 10)];
    [self.dianzanBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 4, 0, -4)];

    
    kWeakSelf(weakSelf);
    [self.measgBtn addActionHandler:^{
        [[weakSelf getCurrentViewController].navigationController pushViewController:[HomeDeliciousFoodDetailController new] animated:YES];

//        PresentationObject * objcet = [PresentationObject sharedPresentationObject];
//        objcet.subViewFrame = CGRectMake(0, 225, KScreenWidth, KScreenHeight - 225);
//        [objcet showPresentingViewController:[weakSelf getCurrentViewController] PresentedViewController:[HomeCommentsController new]];
    }];
    
    
    [self.userView addSubview:self.measgBtn];
    [self.userView addSubview:self.dianzanBtn];
    
    [self.userView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.mas_equalTo(0);
    }];
    [self.dianzanBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.right.mas_equalTo(-16);
        make.width.mas_equalTo(60);
    }];
    
    [self.measgBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.right.mas_equalTo(self.dianzanBtn.mas_left).offset(-10);
        make.width.mas_equalTo(60);

    }];
    
    
    return view;
}

@end
